package com.esunrising.esungsc.service.impl;

import com.esunrising.esungsc.dao.BFCustomItemDao;
import com.esunrising.esungsc.entity.BFCustomItem;
import com.esunrising.esungsc.service.BFCustomeItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Optional;

@Service
public class BFCustomeItemServiceImpl extends BaseServiceImpl<BFCustomItemDao, BFCustomItem> implements BFCustomeItemService {

    @Autowired
    private BFCustomItemDao bfCustomItemDao;

    /**
     * 删除已有GSC账务核算项目数据，再保存
     * @param customItem 需要保存的GSC账务核算项目
     */
    @Override
    @Transactional
    public void syncSave(BFCustomItem customItem){
        // 如果存在，则先删除
        Optional<BFCustomItem> byKeyWord = bfCustomItemDao.findByKeyWord(customItem.getID(), customItem.getFiYear());
        byKeyWord.ifPresent( v -> {
//            bfCustomItemDao.deleteById(v.getID());
            bfCustomItemDao.deleteByKeyWord(v.getID(), v.getFiYear());
        });
        // 保存数据
        super.save(customItem);
    }
}
